package
{
	import starling.display.DisplayObject;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.text.TextField;

	public class CustomSprite extends Sprite
	{
		private var quad:Quad;
		private var legend:TextField;
		private var quadWidth:uint;
		private var quadHeight:uint;
		private var r:Number = 0;
		private var g:Number = 0;
		private var b:Number = 0;
		private var rDest:Number = 0;
		private var gDest:Number = 0;
		private var bDest:Number = 0;
		
		public function CustomSprite($width:Number, $height:Number, $color = 16777215)
		{
			resetColors();
			quadWidth = $width;
			quadHeight = $height;			
			addEventListener(Event.ADDED_TO_STAGE, activate); 
		}
		
		private function activate($e:Event):void
		{
			quad = new Quad(quadWidth, quadHeight);
			legend = new TextField(100, 20, "Hello Starling!", "Arial", 14, 0xFFFFFF);
			addChild(quad);
			addChild(legend);
			pivotX = width >> 1;
			pivotY = height >> 1;
		}
		
	
		private function resetColors():void
		{
			rDest = Math.random() * 255;
			gDest = Math.random() * 255;
			bDest = Math.random() * 255;
		}
		
		public function update():void
		{
			r -= (r - rDest) * .01;
			g -= (g - gDest) * .01;
			b -= (b - bDest) * .01;
			var color:uint = r << 16 | g << 8 | b;
			quad.color = color;
			if(Math.abs(r - rDest) < 1 && Math.abs(g - gDest) < 1 && Math.abs(b - bDest) < 1)
			{
				resetColors();
			}
			rotation += .01;
		}
		
	}
}